﻿Imports System.Windows.Forms
Imports FileCopyExtensions
Imports System.IO

Class MainWindow

    Dim retourCopie As CopyFileCallback
    Dim finCopie As CopyFileTerminate
    Public Delegate Sub MiseAJourProgress_delegate(value As Double)

    Private Sub btn_source_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btn_source.Click
        Dim OpenFileDialog1 As New OpenFileDialog()
        Dim res As DialogResult

        res = OpenFileDialog1.ShowDialog()

        If res = Forms.DialogResult.OK Then
            txt_source.Text = OpenFileDialog1.FileName
        End If

    End Sub

    Private Sub btn_dest_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btn_dest.Click
        Dim openFolder As New FolderBrowserDialog()
        Dim res As DialogResult

        res = openFolder.ShowDialog()

        If res = Forms.DialogResult.OK Then
            txt_dest.Text = openFolder.SelectedPath
        End If
    End Sub

    Private Sub btn_GO_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btn_GO.Click
        Dim fIn As New FileInfo(txt_source.Text)
        Dim fout As New FileInfo(txt_dest.Text + "\\" + fIn.Name)


        retourCopie = New CopyFileCallback(AddressOf MaFonction)
        finCopie = New CopyFileTerminate(AddressOf FinCopy_function)
        FileCopyExtensions.FileCopyExtensions.CopyFile(fIn, fout, CopyFileOptions.None, retourCopie, finCopie)

    End Sub

    Private Function MaFonction(source As FileInfo, destination As FileInfo, state As Object, totalFileSize As Long, totalBytesTransferred As Long) As CopyFileCallbackAction
        Dispatcher.Invoke(New MiseAJourProgress_delegate(AddressOf MiseAJourProgress), totalBytesTransferred / totalFileSize * 100)

        Return CopyFileCallbackAction.Continue
    End Function

    Private Sub MiseAJourProgress(value As Double)
        ProgressBar1.Value = value
        lbl_avancement.Content = value.ToString("0.00") + " %"
    End Sub

    Private Sub FinCopy_function(succes As Boolean)
        If succes Then
            MessageBox.Show("Fin de la copie OK")
        Else
            MessageBox.Show("KO")
        End If


    End Sub


End Class
